
// 226.翻转二叉树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        
        function<TreeNode*(TreeNode*)> invert = [&](TreeNode* pos)->TreeNode*
        {
            if(pos == nullptr) return nullptr;
            TreeNode *top = new TreeNode(pos->val);

            top->left = invert(pos->right);
            top->right = invert(pos->left);
            return top;
        };
        return invert(root);
    }
};